/**
 * 
 */
package com.sdeonline.bible.creencias;

import java.util.ArrayList;
import java.util.TreeSet;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * @author daniel
 *
 */
public class BeliefsListAdapter extends ArrayAdapter<String> {

	private static final int TYPE_ITEM = 0;
    private static final int TYPE_TITLE = 1;
    private static final int TYPE_MAX_COUNT = TYPE_TITLE + 1;
 
    private ArrayList<String> mData = new ArrayList<String>();
    private LayoutInflater mInflater;
    private TreeSet<Integer> mTitlesSet = new TreeSet<Integer>();

    public BeliefsListAdapter(Context context, int textViewResourceId,
		String[] objects) {
		super(context, textViewResourceId, objects);

		// TODO Auto-generated constructor stub
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		

		for (int i = 0; i < objects.length; i++) {
			switch (i) {
				case 0:
				case 6:
				case 9:
				case 13:
				case 22:
				case 28:
					addItem(objects[i]);
					break;
				default:
					addSeparatorItem(objects[i]);
			}
		}
    }
        
    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }
 
    public void addSeparatorItem(final String item) {
        mData.add(item);
        // save separator position
        mTitlesSet.add(mData.size() - 1);
        notifyDataSetChanged();
    }
 
    @Override
    public int getItemViewType(int position) {
        return mTitlesSet.contains(position) ? TYPE_ITEM : TYPE_TITLE;
    }
 
    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }
 
    @Override
    public int getCount() {
        return mData.size();
    }
 
    @Override
    public String getItem(int position) {
        return mData.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	    System.out.println("getView " + position + " " + convertView + " type = " + getItemViewType(position));
		
	    int type = getItemViewType(position);
	    TextView newView = (TextView) convertView;
	    
		if (newView == null) {
		    switch (type) {
		        case TYPE_ITEM:
		            newView = (TextView) mInflater.inflate(R.layout.beliefs_list_item, null);
		            break;
		        case TYPE_TITLE:
		            newView = (TextView) mInflater.inflate(R.layout.beliefs_list_title, null);
		            break;
		    }
		}
		newView.setText(mData.get(position));
		return newView;
	}
	
	@Override
	public boolean isEnabled(int position) {
		return getItemViewType(position) == TYPE_ITEM ? true : false;
    }
 
}
